home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / glibc108.zip / glibc108 / string / strcoll.c < prev    next >
C/C++ Source or Header  |  1991-06-12  |  2KB  |  71 lines

  1. /* Copyright (C) 1991 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3.  
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public License as
  6. published by the Free Software Foundation; either version 2 of the
  7. License, or (at your option) any later version.
  8.  
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12. Library General Public License for more details.
  13.  
  14. You should have received a copy of the GNU Library General Public
  15. License along with the GNU C Library; see the file COPYING.LIB.  If
  16. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  17. Cambridge, MA 02139, USA.  */
  18.  
  19. #include <ansidecl.h>
  20. #include <localeinfo.h>
  21. #include <stddef.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24.  
  25.  
  26. /* Compare S1 and S2, returning less than, equal to or
  27.    greater than zero if the collated form of S1 is lexiographically
  28.    less than, equal to or greater than the collated form of S2.  */
  29. int
  30. DEFUN(strcoll, (s1, s2), CONST char *s1 AND CONST char *s2)
  31. {
  32.   if (_collate_info == NULL || _collate_info->values == NULL)
  33.     return strcmp(s1, s2);
  34.   else
  35.     {
  36.       CONST unsigned char *CONST values = _collate_info->values;
  37.       CONST unsigned char *CONST offsets = _collate_info->offsets;
  38.  
  39.       while (*s1 != '\0' && *s2 != '\0')
  40.     {
  41.       CONST unsigned char c1 = *s1++, c2 = *s2++;
  42.       CONST unsigned char v1 = values[c1], v2 = values[c2];
  43.       CONST unsigned char o1 = offsets[c1], o2 = offsets[c2];
  44.  
  45.       if (v1 == UCHAR_MAX && o1 == 0)
  46.         /* This is a non-collating element.  Skip it.  */
  47.         --s2;
  48.       else if (v2 == UCHAR_MAX && o2 == 0)
  49.         --s1;
  50.       else if (v1 == UCHAR_MAX && o1 == CHAR_MAX)
  51.         {
  52.           /* This element collates lower than anything else.  */
  53.           if (v2 != UCHAR_MAX || o2 != CHAR_MAX)
  54.         return -1;
  55.         }
  56.       else if (v2 == UCHAR_MAX && o2 == CHAR_MAX)
  57.         return 1;
  58.       else if (v1 != v2)
  59.         return v1 - v2;
  60.       else if (o1 != o2)
  61.         return o1 - o2;
  62.     }
  63.  
  64.       if (*s1 == '\0')
  65.     return *s2 == '\0' ? 0 : -1;
  66.       else if (*s2 == '\0')
  67.     return 1;
  68.       return 0;
  69.     }
  70. }
  71.